package com.huaqin.punan.database;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private static final String TAG = "Add";
	private EditText edname, edage, edtall, id;
	private Button add, deleteshow, show, iddelete, idupdate, idshow,
			deleteall;
	private TextView tedatashow;
	private ListView datashow;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		edname = (EditText) findViewById(R.id.edname);
		edage = (EditText) findViewById(R.id.edage);
		edtall = (EditText) findViewById(R.id.edtall);
		id = (EditText) findViewById(R.id.id);
		tedatashow = (TextView) findViewById(R.id.tedatashow);
		datashow = (ListView) findViewById(R.id.datashow);

		add = (Button) findViewById(R.id.add);
		deleteshow = (Button) findViewById(R.id.deleteshow);
		show = (Button) findViewById(R.id.show);
		iddelete = (Button) findViewById(R.id.iddelete);
		idupdate = (Button) findViewById(R.id.idupdate);
		idshow = (Button) findViewById(R.id.idshow);
		deleteall = (Button) findViewById(R.id.deleteall);

		add.setOnClickListener(this);
		deleteshow.setOnClickListener(this);
		show.setOnClickListener(this);
		iddelete.setOnClickListener(this);
		idupdate.setOnClickListener(this);
		idshow.setOnClickListener(this);
		deleteall.setOnClickListener(this);
	}

	public void onClick(View v) {
		/*
		 * 添加数据
		 */
		if (v == add) {
			if (!id.getText().toString().equals(null)
					&& !edname.getText().toString().equals(null)
					&& !edage.getText().toString().equals(null)
					&& !edtall.getText().toString().equals(null)) {
				try {
					UserDAO userdao = new UserDAO(MainActivity.this);
					User user = new User(Integer.valueOf(id.getText()
							.toString()), edname.getText().toString(),
							Integer.valueOf(edage.getText().toString()), edtall
									.getText().toString());
					userdao.add(user);
					Toast.makeText(MainActivity.this, "成功添加！",
							Toast.LENGTH_LONG).show();
					tedatashow.setText("新添数据为：" + "\n" + "ID:"
							+ Integer.valueOf(id.getText().toString()) + ","
							+ "姓名:" + edname.getText().toString() + "," + "年龄:"
							+ Integer.valueOf(edage.getText().toString()) + ","
							+ "身高:" + edtall.getText().toString() + ",");
					// 设置为空
					edage.setText("");
					edname.setText("");
					edtall.setText("");
					id.setText("");

				} catch (Exception e) {
					Log.i(TAG, "出错了");
					Log.i(TAG, e.getMessage());
				}
			} else if (id.getText().toString().equals(null)) {
				Toast.makeText(MainActivity.this, "ID不能为空！", Toast.LENGTH_LONG)
						.show();
			}
		}
		/*
		 * 清除显示
		 */
		if (v == deleteshow) {
			tedatashow.setText("");
		}
		/*
		 * 全部显示
		 */
		if (v == show) {
			try {
				UserDAO userdao = new UserDAO(MainActivity.this);
				Cursor cursor = userdao.select();
				/*
				 * 构建Listview适配器
				 */
				@SuppressWarnings("deprecation")
				SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
						R.layout.showuser, cursor, new String[] { "_id",
								"name", "age", "tall" }, new int[] {
								R.id.textView1, R.id.textView2, R.id.textView3,
								R.id.textView4 });
				datashow.setAdapter(adapter);

			} catch (Exception f) {

				Log.e("err", f.getMessage());
				Log.e("err", null, f.getCause());
				Toast.makeText(MainActivity.this, "显示不了", Toast.LENGTH_LONG)
						.show();
			}
		}

		/*
		 * ID删除
		 */
		if (v == iddelete) {
			UserDAO userDAO = new UserDAO(MainActivity.this);// 注意 不能放在外面
			userDAO.delete(Integer.valueOf(id.getText().toString()));
			Toast.makeText(MainActivity.this,
					"您成功删除了" + Integer.valueOf(id.getText().toString()),
					Toast.LENGTH_LONG).show();
			id.setText("");
		}

		/*
		 * 全部删除
		 */
		if (v == deleteall) {
			UserDAO userDAO = new UserDAO(MainActivity.this);// 注意 不能放在外面
			userDAO.delelteall();
			Toast.makeText(MainActivity.this, "您已经把数据全部删除了", Toast.LENGTH_LONG)
					.show();
		}

		/*
		 * ID更新
		 */
		if (v == idupdate) {
			UserDAO userDAO = new UserDAO(MainActivity.this);// 注意 不能放在外面
			User user = userDAO.find(Integer.valueOf(id.getText().toString()));
			user.setName(edname.getText().toString());
			user.setAge(Integer.valueOf(edage.getText().toString()));
			user.setTall(edtall.getText().toString());
			userDAO.update(user);
			Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_LONG).show();
		}

		/*
		 * ID查询
		 */
		if (v == idshow) {
			UserDAO userDAO = new UserDAO(MainActivity.this);// 注意 不能放在外面
			User user = userDAO.find(Integer.valueOf(id.getText().toString()));
			tedatashow.setText("ID：" + user.getId() + " " + "姓名："
					+ user.getName() + " " + "年龄：" + user.getAge() + " " + "身高"
					+ user.getTall());
		}
		/*
   * 
   */
	}
}